from google.appengine.api import urlfetch
from google.appengine.ext import webapp
import wsgiref.handlers
import simplejson as JSON


class IpDetails(webapp.RequestHandler):
    
    def get(self):
        ip_address = self.request.get("ip")
        response = urlfetch.fetch("http://pipes.yahoo.com/pipes/pipe.run?IPAddress=%s&_id=2pIpcSHt3RGobMCs_w6H4A&_render=json" % ip_address)
        response = response.content.partition('[{"content":"')[2]
        response = response.partition('","description":""')[0]
        
        response = response.replace('\\"','"')
        response = response.replace('\\n',"")
        response = response.replace('\\t',"")
        response = response.replace('\\r',"")
        response = response.replace('\\/','/')

        self.response.out.write("<html><head><link href='/css/ip.css' rel='stylesheet' type='text/css'/></head><body>%s</body></html>" % response)
       
if __name__ == '__main__':
  application = webapp.WSGIApplication(
      [
        ('/.*',IpDetails )
      ],
      debug=True)

  wsgiref.handlers.CGIHandler().run(application) 
